﻿<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
       
    Protected Function addclause(ByVal strInput As String, ByVal toAdd As String) As String
        
        If strInput <> "" Then
            strInput = strInput & " AND "
        End If
        
        addclause = strInput & toAdd
        
    End Function
    
    Protected Function Build_where() As String
        Dim sWhereClause As String = ""
        
        If Utilities.CheckboxListSelections(CBLProgetti) <> "" Then
            sWhereClause = "ProjectCode IN (" + Utilities.CheckboxListSelections(CBLProgetti) + " )"
        End If
        
        If Utilities.CheckboxListSelections(CBLPersone) <> "" Then
            sWhereClause = addclause(sWhereClause, "Persons_id IN (" + Utilities.CheckboxListSelections(CBLPersone) + " )")
        ElseIf Int32.Parse(Session("userLevel")) < 3 Then
            sWhereClause = addclause(sWhereClause, "Persons_id = " & Int32.Parse(Session("persons_id")))
        End If

        If mieore.Checked Then
            sWhereClause = addclause(sWhereClause, "Persons_id = " & Int32.Parse(Session("persons_id")))
        End If
        
        If DDLClienti.SelectedValue <> Nothing Then
            sWhereClause = addclause(sWhereClause, "CodiceCliente = " & ASPcompatility.FormatStringDb(DDLClienti.SelectedValue))
        End If

        If DDLsocieta.SelectedValue <> Nothing Then
            sWhereClause = addclause(sWhereClause, "company_id = " & ASPcompatility.FormatStringDb(DDLsocieta.SelectedValue))
        End If
                
        If freelance.Checked Then
            sWhereClause = addclause(sWhereClause, "company_id <> 1 ")
        End If
        
        If DDLManager.SelectedValue <> Nothing Then
            sWhereClause = addclause(sWhereClause, "idManager = " & ASPcompatility.FormatStringDb(DDLManager.SelectedValue))
        End If
        
        If sWhereClause <> "" Then
            sWhereClause = sWhereClause & " AND "
        End If

        sWhereClause = sWhereClause & _
                       " date >= " & ASPcompatility.FormatDateDb(ASPcompatility.FirstDay(DDLFromMonth.SelectedValue, DDLFromYear.SelectedValue)) & _
                       " AND date <= " & ASPcompatility.FormatDateDb(ASPcompatility.LastDay(DDLToMonth.SelectedValue, DDLToYear.SelectedValue))
        
        Build_where = sWhereClause
    
    End Function
            
    Protected Sub sottometti_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim sWhereClause As String = ""
        
        sWhereClause = Build_where()
        
        If Request("download") <> Nothing Then

            Select Case RBTipoReport.SelectedIndex
            
                Case 0, 2, 4
                    Utilities.ExportXls("Select NomePersona, NomeSocieta, CodiceCliente, NomeCliente, ProjectCode, NomeProgetto, DescTipoProgetto, " & _
                    "NomeManager, fDate, AnnoMese, HourTypeCode, ActivityCode, ActivityName, flagstorno, flagTrasferta, Hours, Giorni, Comment, ActivityCode, ActivityName, AccountingDateAnnoMese from v_ore where " & _
                    sWhereClause)
                Case 1, 3, 5
                    Utilities.ExportXls("Select Persona, NomeSocieta, CodiceCliente, NomeCliente, ProjectCode, NomeProgetto, TipoProgetto, " & _
                    "Manager, fDate, AnnoMese, ExpenseCode, DescSpesa, CreditCardPayed, flagstorno, Invoiceflag,KM, Importo, Comment, AccountingDateAnnoMese from v_spese where " & _
                    sWhereClause)
            End Select
                
        Else
        
            Select Case RBTipoReport.SelectedIndex
            
                Case 0
                    Response.Redirect("/timereport/report/generated/EstraiOresmry.asp?reset=1&whereclause=" & sWhereClause)
                Case 1
                    Response.Redirect("/timereport/report/generated/EstraiSpesesmry.asp?reset=1&whereclause=" & sWhereClause)
                Case 2
                    Response.Redirect("/timereport/report/generated/TotaliOresmry.asp?reset=1&whereclause=" & sWhereClause)
                Case 3
                    Response.Redirect("/timereport/report/generated/TotaliSpesesmry.asp?reset=1&whereclause=" & sWhereClause)
                Case 4
                    Response.Redirect("/timereport/report/generated/CrossGiorniMesectb.asp?reset=1&whereclause=" & sWhereClause)
                Case 5
                    Response.Redirect("/timereport/report/generated/CrossSpeseMesectb.asp?reset=1&whereclause=" & sWhereClause)
            End Select
        End If
        
    End Sub
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
                
        If Not IsPostBack Then
            '           Popola dropdown con i valori                   
            ASPcompatility.SelectYears(DDLFromYear)
            ASPcompatility.SelectYears(DDLToYear)
            ASPcompatility.SelectMonths(DDLFromMonth)
            ASPcompatility.SelectMonths(DDLToMonth)
        End If
        
        ' progetti
        If Session("NoActive") = False Then
            LBProgetti.Text = "> Includi progetti non attivi: [on ]"
        End If

        ' persone
        If Session("NoPersActive") = False Then
            LBPersone.Text = "> Includi persone non attive: [on ]"
        End If       
                
    End Sub
    
    Protected Sub CBLPersone_Load(ByVal sender As Object, ByVal e As System.EventArgs)
                        
        ' limita record visualizzati in base alle autorizzazioni e rende non visible il controllo
        If Int32.Parse(Session("userLevel")) < 3 Then
            Persone.SelectCommand = "SELECT [Persons_id], [Name] FROM [Persons] WHERE ([Active] = @Active) AND Persons_id = " & Int32.Parse(Session("persons_id")) & " ORDER BY [Name]"
            CBLPersone.Visible = False
        End If
        
        If Int32.Parse(Session("userLevel")) = 3 Then
            Persone.SelectCommand = "SELECT DISTINCT ForcedAccounts.Persons_id, Persons.Name FROM Projects INNER JOIN " & _
                                    "ForcedAccounts ON Projects.Projects_Id = ForcedAccounts.Projects_id INNER JOIN " & _
                                    "Persons ON ForcedAccounts.Persons_id = Persons.Persons_id WHERE (Projects.ClientManager_id = " & Int32.Parse(Session("persons_id")) & ")"
            CBLPersone.Visible = True
        End If
        
    End Sub

    Protected Sub CBLProgetti_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
        ' per compatibilità con piattaforme IT/US
        If Session("ForcedAccount") Then
            '						Right join: includes all the forced projects plus the ones with the flag always_available on			
            Progetti.SelectCommand = "SELECT ProjectCode, ([ProjectCode] + ' ' + [Name]) as coddes FROM ForcedAccounts RIGHT JOIN Projects ON ForcedAccounts.Projects_id = Projects.Projects_Id WHERE ( ( ForcedAccounts.Persons_id=" & Session("Persons_id") & " OR Projects.Always_available = 1 ) AND (projects.Active IN (1, @NoActive)) )  ORDER BY Projects.ProjectCode"
        End If
    End Sub

    Protected Sub LBProgetti_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        If Session("NoActive") = 1 Then
            '           selezione anche non attivi e cambia descrizione
            Session("NoActive") = 0
            LBProgetti.Text = "> Includi progetti non attivi: [on ]"
        Else
            '           selezione solo attivi e cambia descrizione
            Session("NoActive") = 1
            LBProgetti.Text = "> Includi progetti non attivi: [off ]"
        End If
        
    End Sub

    Protected Sub LBPersone_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        
        If Session("NoPersActive") = 1 Then
            '           selezione anche non attivi e cambia descrizione
            Session("NoPersActive") = 0
            LBPersone.Text = "> Includi persone non attive: [on ]"
        Else
            '           selezione solo attivi e cambia descrizione
            Session("NoPersActive") = 1
            LBPersone.Text = "> Includi persone non attive: [off ]"
        End If
                
    End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Esporta</title>
    
    <link href="/timereport/include/commoncss.aspx" rel="stylesheet" type="text/css" />

    </head>

<script language=JavaScript src= "/timereport/include/menu/menu_array.js" type=text/javascript></script>
<script language=JavaScript src= "/timereport/include/menu/mmenu.js" type=text/javascript></script>    
    
<body>    
<table width="760" border="0"> 
 <tr> 
    <td height="30" class="BarraTop" align="right">Utente: <%= session("UserName") %> 
    </td>
</table> 
    <form id="form1" runat="server" class="DetailsView" >    
    <div>   
        <table width="600">
            <tr>
                <th>
                    <asp:LinkButton ID="LBProgetti" runat="server" OnClick="LBProgetti_Click" >> Includi progetti non attivi: [off]</asp:LinkButton>
                   </th>
                <th align="right"> 
                    <asp:LinkButton ID="LBPersone" runat="server" OnClick="LBPersone_Click">> Includi persone non attive: [off]</asp:LinkButton>
                   </th>    
                    
            </tr>
            <tr>
                <td rowspan="2">
                        <div style="overflow: auto; height: 160px; width: 295px;">
                    <asp:CheckBoxList ID="CBLProgetti" runat="server" DataSourceID="Progetti" 
                        DataTextField="coddes" DataValueField="ProjectCode" OnDataBinding="CBLProgetti_DataBinding">
                    </asp:CheckBoxList>
                    <asp:SqlDataSource ID="Progetti" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:MSSql12155ConnectionString %>" 
                        
                                
                                SelectCommand="SELECT ProjectCode, ProjectCode + ' ' + Name AS coddes FROM Projects WHERE (Active IN (1, @NoActive)) ORDER BY ProjectCode">
                        <SelectParameters>
                            <asp:SessionParameter DefaultValue=1 Name="NoActive" 
                                SessionField="noactive" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                    </div>    </td>
                <td >
                                <asp:CheckBox ID="mieore" runat="server"  
                                    Text="&gt;&gt; Seleziona i miei consuntivi" Font-Bold="False" />
                </td>
            </tr>
            <tr>
                <td style="padding-left: 0px">
                  <div style="overflow: auto; height: 140px; width: 295px;">
                    <asp:CheckBoxList ID="CBLPersone" runat="server" DataSourceID="Persone" 
                        DataTextField="Name" DataValueField="Persons_id"  OnLoad="CBLPersone_Load">
                    </asp:CheckBoxList>
                    <asp:SqlDataSource ID="Persone" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:MSSql12155ConnectionString %>" 
                        
                          SelectCommand="SELECT [Persons_id], [Name] FROM [Persons] WHERE (Active IN (1, @NoActive)) ORDER BY [Name]">
                        <SelectParameters>
                            <asp:SessionParameter DefaultValue=1 Name="NoActive" 
                                SessionField="NoPersActive" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                    </div> 
                </td>
            </tr>
                        <tr>
                <td colspan="2">
                            </td>
            </tr>
                    </table>
                    <table width="600">
                        <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
                        <tr>
                <td>
                    Cliente</td>
                <td>
                    <asp:DropDownList ID="DDLClienti" runat="server" DataSourceID="Clienti" 
                        DataTextField="coddes" DataValueField="CodiceCliente" 
                        AppendDataBoundItems="True">            
                        <asp:ListItem  Value="" Text="Selezionare un valore"/>
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="Clienti" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:MSSql12155ConnectionString %>" 
                        SelectCommand="SELECT [CodiceCliente], ([CodiceCliente] + ' ' + [Nome1]) as coddes FROM [Customers] WHERE ([FlagAttivo] = @FlagAttivo) ORDER BY [CodiceCliente]">
                        <SelectParameters>
                            <asp:Parameter DefaultValue=1 Name="FlagAttivo" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </td>
            </tr>
            <tr>
                <td>
                    Manager</td>
                <td>
                    <asp:DropDownList ID="DDLManager" runat="server" DataSourceID="Manager" 
                        DataTextField="Name" DataValueField="Persons_id" AppendDataBoundItems="True">
                        <asp:ListItem  Value="" Text="Selezionare un valore"/>  
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="Manager" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:MSSql12155ConnectionString %>" 
                        SelectCommand="SELECT DISTINCT Persons.Persons_id, Persons.Name FROM Persons INNER JOIN Projects ON Persons.Persons_id = Projects.ClientManager_id WHERE (Persons.Active = @Active) ORDER BY Persons.Name">
                        <SelectParameters>
                            <asp:Parameter DefaultValue=1 Name="Active" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </td>
            </tr>
            <tr>
                <td>
                    Società</td>
                <td>
                                        <asp:DropDownList ID="DDLsocieta" runat="server" DataSourceID="societa" 
                        DataTextField="Name" DataValueField="Company_id" 
                        AppendDataBoundItems="True">
                    <asp:ListItem  Value="" Text="Selezionare un valore"/>                            
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="societa" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:MSSql12155ConnectionString %>" 
                        SelectCommand="SELECT [Company_id], [Name] FROM [Company] ORDER BY [Name]">
                    </asp:SqlDataSource>
                                <asp:CheckBox ID="freelance" runat="server" 
                                    Text="Tutte le risorse non Aeonvis" />
                </td>
            </tr>
          <tr> 
            <td>&nbsp;</td>
            <td> &nbsp;</td>
          </tr>
          <tr> 
            <td>Dalla data:</td>
            <td> <asp:DropDownList  runat="server" id="DDLFromMonth"> </asp:DropDownList>
                 <asp:DropDownList  runat="server" id="DDLFromYear"></asp:DropDownList> </td>
          </tr>
          <tr> 
            <td>Alla data:</td>
            <td> <asp:DropDownList  runat="server" ID="DDLToMonth"></asp:DropDownList>
            <asp:DropDownList   runat="server" ID="DDLToYear"> </asp:DropDownList> </td>
          </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <th colspan="2">
                    &nbsp;Report</th>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:RadioButtonList ID="RBTipoReport" runat="server" RepeatColumns="3">
                        <asp:ListItem Selected="True" Value="1">Dettaglio ore</asp:ListItem>
                        <asp:ListItem Value="2">Dettaglio Spese</asp:ListItem>
                        <asp:ListItem Value="3">Totali ore</asp:ListItem>                       
                        <asp:ListItem Value="4">Totali Spese</asp:ListItem>
                        <asp:ListItem Value="5">Giornate x mese</asp:ListItem>
                        <asp:ListItem Value="6">Spese x mese</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
            </tr>
            
            </table>
                        <br />
    &nbsp;<asp:Button ID="sottometti" runat="server" Text="Genera report" 
    CssClass="FormButton" OnClick="sottometti_Click" />    &nbsp;<asp:Button ID="download" runat="server" Text="Download file xls" 
    CssClass="FormButton" OnClick="sottometti_Click" />    
                </div>
    </form>
</body>
</html>
